package com.app.service.impl;

/*
  @author 阿伟
  @create 2020-11-24:15:04
  逻辑层的复杂方法实现
*/

import com.app.condition.ConsultCondition;
import com.app.dao.ConsultDao;
import com.app.dao.UserDao;
import com.app.service.ConsultService;
import com.app.service.OssService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.Code;
import com.common.entity.Consult;
import com.common.entity.ResponseResult;
import com.common.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.regex.Pattern;

@Service
@Transactional
public class ConsultServiceImpl extends ServiceImpl<ConsultDao, Consult> implements ConsultService {


    @Autowired
    OssService ossService;

    @Autowired
    ConsultDao consultDao;

    @Autowired
    UserDao userDao;

    //分页查询
    @Override
    public void selectConsultByLimit(Page<Consult> page, ConsultCondition consultCondition) {

        //分页查询条件器
        QueryWrapper<Consult> queryWrapper = new QueryWrapper<>();
        //判断是否有查询的条件
        if(consultCondition == null){
            //没有条件则返回普通分页查询
            baseMapper.selectPage(page,queryWrapper);
            return;
        }

        //查询的条件
        String consultText = consultCondition.getConsultText();
        String userNickname = consultCondition.getUserNickname();
        String createTime = consultCondition.getCreateTime();
        String modifyTime = consultCondition.getModifyTime();
        //判断条件是否为空
        if(!StringUtils.isEmpty(consultText)){
            queryWrapper.like("consult_text",consultText);
        }
        if(!StringUtils.isEmpty(userNickname)){
            queryWrapper.like("user_id",userNickname);
        }
        if(!StringUtils.isEmpty(createTime)){
            queryWrapper.like("consult_createdate",createTime);
        }
        if(!StringUtils.isEmpty(modifyTime)){
            queryWrapper.like("consult_updatedate",modifyTime);
        }
        //分页和查询条件
        baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public ResponseResult addConsult(MultipartFile img, String text, String phone) {
        /**
         * 正则表达式：验证手机号
         */
        final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        //验证
        if(!Pattern.matches(REGEX_MOBILE,phone)){
            return ResponseResult.error(Code.REQUEST_ERROR).Data("error","输入正确的手机号");
        }
        if(org.apache.commons.lang.StringUtils.isBlank(text)){
           return ResponseResult.success(Code.REQUEST_ERROR).Data("error","请求参数有误") ;
        }
        //将当前登陆的用户的手机号查询出来的id给consult表
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_phone",phone);
        User user = userDao.selectOne(wrapper);
        String url = null;
        //图片上传
        if(img != null){
            url = ossService.uploadAliyun(img);
        }
        Consult consult = new Consult();
        consult.setConsultId(0);
        consult.setConsultImg(url);
        consult.setConsultText(text);
        consult.setUserId(user.getUserId());
        consult.setConsultLogicflage(0);
        consult.setConsultVersion(1);
        consult.setConsultCreatedate(null);
        consult.setConsultUpdatedate(null);
        int insert = consultDao.insert(consult);
        if(insert != 1){
            return ResponseResult.error(Code.REQUEST_ERROR).Data("msg","插入失败");
        }
        //如果用户有上传图片，那么返回上传图片后的地址
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("imgUrl",url);
    }

    @Override
    public ResponseResult getAllConsult() {
        List<Consult> consults = consultDao.selectAllConsult();
        if(consults != null){
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data("consult",consults);
        }
        return ResponseResult.error(Code.REQUEST_ERROR).Data("msg",null);
    }
}
